package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.common.security.auth.AuthUtil;
import com.kws.merchant.domain.AfterSalesHistory;
import com.kws.merchant.domain.AfterSalesWarranty;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.ContractSignature;
import com.kws.merchant.domain.po.AfterSalesWarrantyPo;
import com.kws.merchant.domain.vo.AfterSalesWarrantyVo;
import com.kws.merchant.mapper.AfterSalesWarrantyMapper;
import com.kws.merchant.service.AfterSalesHistoryService;
import com.kws.merchant.service.AfterSalesWarrantyService;
import com.kws.merchant.service.MerchantService;
import com.kws.merchant.util.FileUtil;
import com.kws.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 售后-保修单(AfterSalesWarranty)表服务实现类
 *
 * @author yxy
 * @since 2024-10-22 19:23:10
 */
@Service("afterSalesWarrantyService")
public class AfterSalesWarrantyServiceImpl extends ServiceImpl<AfterSalesWarrantyMapper, AfterSalesWarranty> implements AfterSalesWarrantyService {

    @Resource
    private AfterSalesWarrantyMapper afterSalesWarrantyMapper;
    @Autowired
    private AfterSalesHistoryService afterSalesHistoryService;
    @Value("${minio.ENDPOINT}")
    private String minioUrl;

    /**
     * 分页查询列表
     *
     * @param afterSalesWarrantyPo
     * @return
     */
    @Override
    public Page<AfterSalesWarranty> pageList(AfterSalesWarrantyPo afterSalesWarrantyPo) {
        Integer pageSize = afterSalesWarrantyPo.getPageSize();
        Integer totalPage = afterSalesWarrantyPo.getTotalPage();
        String warrantyNo = afterSalesWarrantyPo.getWarrantyNo();//保修单号
        String type = afterSalesWarrantyPo.getType();//0设备 1商品服务
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        //查询是否为商家
        boolean flag = sysUser.getCompanyId()!=null;
        //查询自己的签名和盖章
        String sql = " ";
        if (flag) {
            sql = " a.merchant_id=" + sysUser.getCompanyId();
        }
        if (!StringUtils.isEmpty(warrantyNo)) {
            sql += " and a.warranty_no='" + warrantyNo+"'";
        }
        if (!StringUtils.isEmpty(type)) {
            sql += " and a.type=" + type;
        }
        int total = afterSalesWarrantyMapper.countToTal(sql);
        int sizeNum = (pageSize - 1) * totalPage;//计算分页
        //查询签名数据
        List<AfterSalesWarranty> records = afterSalesWarrantyMapper.getRecords(sql, sizeNum, totalPage);
        records.forEach(x->{
            x.setImg(FileUtil.fixFileName()+x.getImg());
        });
        Page<AfterSalesWarranty> page = new Page<>();
        page.setRecords(records);
        page.setTotal(total);
        page.setCurrent(pageSize);
        page.setPages((total + totalPage - 1) / totalPage);
        page.setSize(totalPage);
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id
     * @return
     */
    @Override
    public AfterSalesWarranty detailById(Long id) {
        AfterSalesWarranty afterSalesWarranty = afterSalesWarrantyMapper.detailById(id);
        if (ObjectUtils.isEmpty(afterSalesWarranty)) {
            throw new RuntimeException("无法查询该保修单");
        }
        String warrantyNo = afterSalesWarranty.getWarrantyNo();
        afterSalesWarranty.setImg(FileUtil.fixFileName() + afterSalesWarranty.getImg());
//        List<AfterSalesHistory> warrantyHistoryList = afterSalesHistoryService.listByWarrantyNo(warrantyNo);
//        AfterSalesWarrantyVo afterSalesWarrantyVo = new AfterSalesWarrantyVo();
//        afterSalesWarrantyVo.setAfterSalesInfo(afterSalesWarranty);
//        afterSalesWarrantyVo.setWarrantyHistoryList(warrantyHistoryList);
        return afterSalesWarranty;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(List<Long> idList) {
//        afterSalesHistoryService.removeByAfterSalesNo("BX", idList);
        this.removeByIds(idList);
        return true;
    }

    /**
     * 保修处理
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean dispose(AfterSalesWarranty afterSalesWarranty) {
        afterSalesWarranty.setImg(FileUtil.subFileName(afterSalesWarranty.getImg()));
        afterSalesWarranty.setStatus(1);//已完成
        return this.updateById(afterSalesWarranty);
    }

    /**
     * 新增保修单
     * @param afterSalesWarranty
     * @return
     */
    @Override
    public boolean addItem(AfterSalesWarranty afterSalesWarranty) {
        SysUser sysUser = AuthUtil.getLoginUser().getSysUser();
        Long companyId = sysUser.getCompanyId();
        afterSalesWarranty.setMerchantId(companyId);
        afterSalesWarranty.setWarrantyNo("BX"+ UUID.randomUUID());
        afterSalesWarranty.setStatus(0);//新增单子默认为未处理
        return this.save(afterSalesWarranty);
    }

    private AfterSalesWarranty getOneByWarrantyNo(String afterSalesNo) {
        return this.getOne(Wrappers.<AfterSalesWarranty>lambdaQuery().eq(AfterSalesWarranty::getWarrantyNo, afterSalesNo));
    }


}
